package com.xydtech.ecard.ecard.service.impl;

import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.ecard.domain.*;
import com.xydtech.ecard.ecard.mapper.*;
import com.xydtech.ecard.ecard.service.IEcardApiMerchantReservationTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 电子名片商户预约任务Service业务层处理
 *
 * @author xydtech
 * @date 2023-08-05
 */
@Service
public class EcardApiMerchantReservationTaskServiceImpl implements IEcardApiMerchantReservationTaskService
{
    @Autowired
    private EcardApiMerchantReservationTaskMapper ecardApiMerchantReservationTaskMapper;
    @Autowired
    private EcardApiMerchantInfoMapper ecardApiMerchantInfoMapper;
    @Autowired
    private EcardApiEnterpriseInfoMapper ecardEnterpriseInfoMapper;
    @Autowired
    private EcardApiAgricultureInfoMapper ecardAgricultureInfoMapper;
    @Autowired
    private EcardApiPersonInfoMapper ecardPersonInfoMapper;

    /**
     * 查询电子名片个人/商户/企业/新型农业主体预约任务
     *
     * @param id 电子名片商户预约任务主键
     * @return 电子名片商户预约任务
     */
    @Override
    public EcardApiMerchantReservationTask selectEcardMerchantReservationTaskById(Long id)
    {
        return ecardApiMerchantReservationTaskMapper.selectEcardMerchantReservationTaskById(id);
    }

    /**
     * 查询电子名片商户预约任务列表
     *
     * @param ecardApiMerchantReservationTask 电子名片商户预约任务
     * @return 电子名片商户预约任务
     */
    @Override
    public List<EcardApiMerchantReservationTask> selectEcardMerchantReservationTaskList(EcardApiMerchantReservationTask ecardApiMerchantReservationTask)
    {
        return ecardApiMerchantReservationTaskMapper.selectEcardMerchantReservationTaskList(ecardApiMerchantReservationTask);
    }

    /**
     * 新增电子名片商户预约任务
     *
     * @param ecardApiMerchantReservationTask 电子名片商户预约任务
     * @return 结果
     */
    @Override
    public int insertEcardMerchantReservationTask(EcardApiMerchantReservationTask ecardApiMerchantReservationTask)
    {
        return ecardApiMerchantReservationTaskMapper.insertEcardMerchantReservationTask(ecardApiMerchantReservationTask);
    }

    /**
     * 修改电子名片个人/商户/企业/新型农业主体预约任务
     *
     * @param ecardApiMerchantReservationTask 电子名片商户预约任务
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateEcardMerchantReservationTask(EcardApiMerchantReservationTask ecardApiMerchantReservationTask)
    {
        String merchantType = ecardApiMerchantReservationTask.getMerchantType();//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
        //修改商户办理成功的业务,添加到商户业务字段里面 1 代表营销成功
        if("1".equals(ecardApiMerchantReservationTask.getResult()) && ecardApiMerchantReservationTask.getActualBusiType() != null){
            //商户类型：1-个人，2-商户，3-企业，4-新型农业主体加判断
            if ("1".equals(merchantType)) {//个人
                EcardApiPersonInfo ecardApiPersonInfo = ecardPersonInfoMapper.selectEcardPersonInfoById(ecardApiMerchantReservationTask.getMerchantId());
                ecardApiPersonInfo.setPerBusinessLabel(ecardApiPersonInfo.getPerBusinessLabel()+","+ecardApiMerchantReservationTask.getActualBusiType());
                ecardPersonInfoMapper.addPersonInfo(ecardApiPersonInfo);
            }
            if ("2".equals(merchantType)) {//商户
                EcardApiMerchantInfo ecardApiMerchantInfo = ecardApiMerchantInfoMapper.selectEcardMerchantInfoById(ecardApiMerchantReservationTask.getMerchantId());
                ecardApiMerchantInfo.setBusinessLabel(ecardApiMerchantInfo.getBusinessLabel()+","+ecardApiMerchantReservationTask.getActualBusiType());
                ecardApiMerchantInfoMapper.addMerchantInfo(ecardApiMerchantInfo);
            }
            if ("3".equals(merchantType)) {//企业
                EcardApiEnterpriseInfo ecardApiEnterpriseInfo = ecardEnterpriseInfoMapper.selectEcardEnterpriseInfoById(ecardApiMerchantReservationTask.getMerchantId());
                ecardApiEnterpriseInfo.setEntBusinessLabel(ecardApiEnterpriseInfo.getEntBusinessLabel()+","+ecardApiMerchantReservationTask.getActualBusiType());
                ecardEnterpriseInfoMapper.addEnterpriseInfo(ecardApiEnterpriseInfo);
            }
            if ("4".equals(merchantType)) {//新型农业主体
                EcardApiAgricultureInfo ecardApiAgricultureInfo = ecardAgricultureInfoMapper.selectEcardAgricultureInfoById(ecardApiMerchantReservationTask.getMerchantId());
                ecardApiAgricultureInfo.setAgrBusinessLabel(ecardApiAgricultureInfo.getAgrBusinessLabel()+","+ecardApiMerchantReservationTask.getActualBusiType());
                ecardAgricultureInfoMapper.addAgricultureInfo(ecardApiAgricultureInfo);
            }
        }

        return ecardApiMerchantReservationTaskMapper.updateEcardMerchantReservationTask(ecardApiMerchantReservationTask);
    }

    /**
     * 批量删除电子名片商户预约任务
     *
     * @param ids 需要删除的电子名片商户预约任务主键
     * @return 结果
     */
    @Override
    public int deleteEcardMerchantReservationTaskByIds(String ids)
    {
        return ecardApiMerchantReservationTaskMapper.deleteEcardMerchantReservationTaskByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除电子名片商户预约任务信息
     *
     * @param id 电子名片商户预约任务主键
     * @return 结果
     */
    @Override
    public int deleteEcardMerchantReservationTaskById(Long id)
    {
        return ecardApiMerchantReservationTaskMapper.deleteEcardMerchantReservationTaskById(id);
    }

    /***
     * 查询个人/商户/企业/新型农业主体预约任务列表
     * @param cmRegistId
     * @param merchantType
     * @param search 查询条件
     * @return: java.util.List<com.xydtech.ecard.ecard.domain.EcardApiMerchantReservationTask>
     * @Author: lin
     * @Date: 2023/8/5 13:07
     */
    @Override
    public List<EcardApiMerchantReservationTask> getMerchantReservationTaskList(Long cmRegistId, String merchantType, String search, Long deptId, String phone, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(phone)) {
                phone = (AESEncryptionUtils.encrypt(phone, salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardApiMerchantReservationTaskMapper.getMerchantReservationTaskList(cmRegistId, merchantType, search, deptId, phone);
    }
}
